import {
  siteCount,
  workflowCount,
  workflowInfo,
  patrolPersonCount,
  patrolPersonInfo,
  workflowExtList,
} from 'api/patrolSpecial';

export default {
  namespace: 'patrol',
  state: {
    station:[],
    station_XData:[],
    station_patrolNumbers:[],
    station_noPatrolNumbers:[]
  },

  effects: {
    *station({ payload }, { call, put }) {
      const result = yield call(siteCount, payload);
      if (result && result.success) {
        yield put({
          type: 'setState',
          payload: {
            station: result.data.list,
            station_XData: result.data.stations,
            station_patrolNumbers: result.data.station_patrolNumbers,
            station_noPatrolNumbers: result.data.station_noPatrolNumbers
          },
        });
      } else {
        throw data;
      }
    },
    *line({ payload }, { call, put }) {
      const result = yield call(workflowCount, payload);
      if (result && result.success) {
        yield put({
          type: 'setState',
          payload: {
            line: result.data.list,
            line_XData: result.data.lines,
            line_patrolLineNumbers: result.data.line_patrolLineNumbers,
            line_noPatrolStationNumbers: result.data.line_noPatrolStationNumbers,
            line_reportNumbers: result.data.line_reportNumbers
          },
        });
        return result;
      } else {
        throw result
        return result;
      }
    },
    *workflowInfo({ payload }, { call, put }) {
      const result = yield call(workflowInfo, payload);
      if (result && result.success) {
        yield put({
          type: 'setState',
          payload: {
            workflowInfo: result.data,
          },
        });
        return result;
      } else {
        throw result
        return result;
      }
    },
    *person({ payload }, { call, put }) {
      const result = yield call(patrolPersonCount, payload);
      if (result && result.success) {
        yield put({
          type: 'setState',
          payload: {
            person: result.data.list,
            person_XData: result.data.persons,
            person_patrolLineNumbers: result.data.person_patrolLineNumbers,
            person_patrolStationNumbers: result.data.person_patrolStationNumbers,
            person_reportNumbers: result.data.person_reportNumbers
          },
        });
        return result;
      } else {
        throw result
        return result;
      }
    },
    *patrolPersonInfo({ payload }, { call, put }) {
      const result = yield call(patrolPersonInfo, payload);
      if (result && result.success) {
        yield put({
          type: 'setState',
          payload: {
            patrolPersonInfo: result.data,
          },
        });
        return result;
      } else {
        throw result
        return result;
      }
    },
    *workflowExtList({ payload }, { call, put }) {
      const result = yield call(workflowExtList, payload);
      if (result && result.success) {
        yield put({
          type: 'setState',
          payload: {
            workflowExtList: result.data,
          },
        });
        return result;
      } else {
        throw result
        return result;
      }
    },
  },

  reducers: {
    setState(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
